package com.smm.web.vmc.service.impl;

import com.smm.web.vmc.dao.BaseDao;
import com.smm.web.vmc.domain.Receipt;
import com.smm.web.vmc.domain.relation.CameraReceiptRelation;
import com.smm.web.vmc.service.CameraReceiptRelationService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/9.
 *
 *
 */
@Service
public class CameraReceiptRelationServiceImpl implements CameraReceiptRelationService {

    @Resource
    private BaseDao<CameraReceiptRelation> cameraReceiptRelationBaseDao;


    @Override
    public void add(CameraReceiptRelation crr) throws Exception {

        cameraReceiptRelationBaseDao.add(crr);
    }

    @Override
    public void deleteAllByReceipt(String receiptNo) throws Exception {

        List<CameraReceiptRelation> relations = this.getAllByReceipt(receiptNo);

        for(CameraReceiptRelation relation: relations){

            cameraReceiptRelationBaseDao.delete(relation);
        }

    }

    @Override
    public List<CameraReceiptRelation> getAllByReceipt(String receiptNo) {

        DetachedCriteria where = DetachedCriteria.forClass(CameraReceiptRelation.class);
        where.add(Restrictions.eq("receipt.no", receiptNo));

        return cameraReceiptRelationBaseDao.query(where);
    }

    @Override
    public List<CameraReceiptRelation> getAllByCamera(int cameraId) {

        DetachedCriteria where = DetachedCriteria.forClass(CameraReceiptRelation.class);
        where.add(Restrictions.eq("camera.id", cameraId));

        return cameraReceiptRelationBaseDao.query(where);
    }


}
